<template>
  <div>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      element-loading-text="给我一点时间"
      border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column width="80" align="center" label="消防设施图像">
        <template slot-scope="scope">
          <embed :src="scope.row.imageUrl" style="height:auto;width:100%;">
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="消防设施名称">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="位置">
        <template slot-scope="scope">
          <span>{{ scope.row.properties.Alocation }}</span>
        </template>
      </el-table-column>
      <el-table-column width="200" align="center" label="消防设施类型ID">
        <template slot-scope="scope">
          <span>{{ scope.row.properties.assetcategoryid }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('table.actions')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetails(scope.row)">{{ $t('table.details') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page.sync="listQuery.page"
        :page-sizes="[10,20,30, 50]"
        :page-size="listQuery.pagesize"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="消防设施名称" prop="name">
          <el-input v-model="temp.name"/>
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="temp.longitude"/>
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="temp.latitude"/>
        </el-form-item>
        <el-form-item label="高度" prop="elevation">
          <el-input v-model="temp.elevation"/>
        </el-form-item>
        <el-form-item label="站点ID" prop="siteid">
          <el-input v-model="temp.siteid"/>
        </el-form-item>
        <el-form-item label="位置图片" prop="spotimage">
          <el-input v-model="temp.spotimage"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{ $t('table.confirm') }}</el-button>
        <el-button v-if="dialogStatus=='update'" type="primary" @click="updateData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { facilitiesList } from '@/api/fire/asset/asset'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'FacilitiesList',
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        pagesize: 10,
        name: '',
        siteid: '',
        arange: this.$route.name
      },
      showReviewer: false,
      temp: {
        assetid: '',
        name: '',
        siteid: '',
        spotimage: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        details: '详情'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        name: [{ required: true, message: '请输入公司地址', trigger: 'blur' }],
        address: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        longitude: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        latitude: [{ required: true, message: '请输入网站', trigger: 'blur' }],
        levation: [{ required: true, message: '请输入网站', trigger: 'blur' }],
        province: [{ required: true, message: '请输入法人', trigger: 'blur' }]
      },
      dialogFormRolesVisible: false,
      btn_query: false,
      btn_add: false,
      btn_edit: false,
      btn_delete: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      facilitiesList(this.listQuery).then(response => {
        this.list = response.data.context.assetlists
        this.total = response.data.context.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.pagesize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        assetid: '',
        id: '',
        name: '',
        siteid: '',
        spotimage: ''
      }
    },
    handleDetails(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'details'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    }
  }
}
</script>
